#coding=utf-8
#校园好声音投票
import requests
import time
import random
USER_COUNT=0
VOTE_COUNT=0
login_url="http://voice.yiban.cn/web/login"
vote_url="http://voice.yiban.cn/vr/activity/vote"
toid=[5022341,5173669,6926123,1746376,7006739]
headers={
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36',
}
account_file=open('data.txt')#账号密码文件夹
account_text=account_file.readlines()
def get_cookies(user):
    login=requests.get(login_url,params=user,headers=headers)
    if "1000" in login.text:
        print user['account']+u"账号登陆成功！"
        cookies=login.cookies
        return cookies
    elif "5700" in login.text:
        print user['account'] +u"账号或密码错误！"
        return False
    else:
        print u"未知错误！"
        return False
def voteing(cookies):
    global VOTE_COUNT
    global USER_COUNT
    for id in toid:
        vote_data={'toid':str(id)}
        time.sleep(random.randint(0,2))
        res=requests.get(vote_url,params=vote_data,cookies=cookies,headers=headers)
        if '5717' in res.text:
            VOTE_COUNT+=1
        elif '0' in res.text:
            VOTE_COUNT+=1
        elif '5718' in res.text:
            VOTE_COUNT+=1
    USER_COUNT+=1
    print u"投票结束"
if __name__ == '__main__':
    for userdata in account_text:
        user={
        'account':str(userdata.split(" ")[0]), #账号
        'pwd':str(userdata.split(" ")[1].replace('\n','')),     #密码
        }
        cookies=get_cookies(user)
        if cookies:
            myvote=voteing(cookies)
    print u"一共有"+str(USER_COUNT)+u"个账号投票成功"
    print u"一共投了"+str(VOTE_COUNT)+u"票"